#include<iostream>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<unistd.h>
using namespace std;

int main()
{
    //创建共享内存
    int key=ftok(".",3962);
    if(key<0)
    {
        cerr<<"ftok failed"<<endl;
        exit(EXIT_FAILURE);
    }
    int shmid=shmget(key,1024,IPC_CREAT);
    if(shmid<0)
    {
        cerr<<"shmget failed"<<endl;
        exit(EXIT_FAILURE);
    }

    //关联共享内存
    char* addr=(char*)shmat(shmid,nullptr,0);
    if(addr==(char*)-1)
    {
        cerr<<"shmat failed"<<endl;
        exit(EXIT_FAILURE);
    }
    //进行通信
    int i=0;
    while(i++<26)
    {    
        cout<<"client say:#"<<addr<<endl;
        sleep(1);
    }
    //去关联共享内存
    int num=shmdt(addr);
    if(num<0)
    {
        cerr<<"shmdt failed"<<endl;
        exit(EXIT_FAILURE);
    }
    return 0;
}




